import { Injectable } from '@nestjs/common';

@Injectable()
export class RandomCodeGenerator {
  /**
   * 生成指定长度的随机验证码
   * @param length 验证码的长度
   */
  generateCode(length: number): string {
    const alphabet =
      '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    let code = '';

    for (let i = 0; i < length; i++) {
      code += alphabet[Math.floor(Math.random() * alphabet.length)];
    }
    return code;
  }

  generateNumCode(length: number) {
    return String(Math.floor(Math.random() * 1000000)).padStart(length, '0');
  }
}
